//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System.Configuration;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Resource.Configuration;
using EntLibContrib.Resource.Providers.Tests;
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Resource.Configuration.Tests
{
	[TestClass]
	public class ResourceManagerDataFixture
	{
		private const string name1 = "name1";
		private const string name2 = "name2";
		private const string baseName = "base name";
		private const string providerName = "resource provider";

		[TestMethod]
		public void CanDeserializeSerializedConfiguration()
		{
			ResourceManagerData data1 = new ResourceManagerData(name1, providerName);
			ResourceManagerData data2 = new ResourceManagerData(name2, providerName);

			ResourceManagerSettings settings = new ResourceManagerSettings();
			settings.DefaultResourceManager = name1;

			settings.ResourceManagers.Add(data1);
			settings.ResourceManagers.Add(data2);

			// needed to save configuration
			settings.ResourceProviders.Add(new CustomResourceProviderData(providerName, baseName, typeof(MockCustomResourceProvider)));

			IDictionary<string, ConfigurationSection> sections = new Dictionary<string, ConfigurationSection>();
			sections[ResourceManagerSettings.SectionName] = settings;
			IConfigurationSource configurationSource = ConfigurationTestHelper.SaveSectionsInFileAndReturnConfigurationSource(sections);

			ResourceManagerSettings resourceManagerSettings = (ResourceManagerSettings)configurationSource.GetSection(ResourceManagerSettings.SectionName);

			Assert.IsNotNull(resourceManagerSettings);
			Assert.AreEqual(2, resourceManagerSettings.ResourceManagers.Count);
			Assert.AreEqual(name1, resourceManagerSettings.DefaultResourceManager);

			Assert.IsNotNull(resourceManagerSettings.ResourceManagers.Get(name1));
			Assert.AreEqual(name1, resourceManagerSettings.ResourceManagers.Get(name1).Name);
			Assert.AreEqual(providerName, resourceManagerSettings.ResourceManagers.Get(name1).ResourceProvider);

			Assert.IsNotNull(resourceManagerSettings.ResourceManagers.Get(name2));
			Assert.AreEqual(name2, resourceManagerSettings.ResourceManagers.Get(name2).Name);
			Assert.AreEqual(providerName, resourceManagerSettings.ResourceManagers.Get(name2).ResourceProvider);
		}
	}
}